<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goodsreport_m extends CI_Model {
	function __construct()
    {
        parent::__construct();
		$this->size = array('25','30','35','40','45','50','55','60');
		$this->sizest = array('25'=>'XXS','30'=>'XS','35'=>'S','40'=>'M','45'=>'L','50'=>'XL','55'=>'XXL','60'=>'XXXL');
    }
	
	/************************
		获取尺寸定制衣服的销量
	*************************/
	function getcustom($custom=2,$goods_sku)
	{
		$daylist = $this->getdata();
	
		foreach($daylist as $key=>$rs)
		{
			$sql = " SELECT sum(orderNum) as orderNums FROM db_factory_sell A LEFT JOIN db_factory_order B ON A.orderId = B.id WHERE A.goods_sku='".$goods_sku."' AND custom = 2 and orderTime >='".$rs['starttime']."' AND orderTime <='".$rs['endtime']."' AND B.status !=5 AND B.updateflag = 9 AND B.orderTime >='2017-05-23 18:20:01'";
			$res = $this->db->get_one($sql);
			$datalist[]['orderNums']  = $res['orderNums'];
		}
		return $datalist;
	}

	//custom 1均码 2定制
	//model  1欧美 2亚洲
	//gender 1男 2女 3通用
	function getalldata($limit,$custom,$model,$gender)
	{
		$sql= "SELECT goods_sku,skuName,skuID FROM `db_factory_sell` WHERE 1 and length(goods_sku)=8 AND (locate(11,goods_sku)=1 or locate(12,goods_sku)=1 ) AND custom= $custom AND model=$model GROUP by goods_sku".$limit;
	
		$query = $this->db->query($sql);
        $goodslist = $query->result_array();
		if(count($goodslist)==0)
		{
			return 200;
		}
		foreach($goodslist as $key=>$rsdb)
		{
			//欧美 1男
			$customNum = $this->getEuropeorder($gender,$rsdb['goods_sku']);
			foreach($customNum as $key=>$rs)
			{
				$num1 = $rs['orderNums'][0]['orderNums'];
				$num2 = $rs['orderNums'][1]['orderNums'];
				$num3 = $rs['orderNums'][2]['orderNums'];
				if($num1==0 && $num2 ==0 && $num3==0)
				{
					continue;
				}
					$post="";
					$post['goos_sku'] = $rsdb['goods_sku'];
					$post['goods_name'] = $rsdb['skuName'];
					$post['goods_id'] = $rsdb['skuID'];
					$post['custom'] =$custom;
					$post['model'] =$model;
					$post['size'] =$rs['size'];
					$post['gender'] =$gender;
					$post['num1'] =$num1;
					$post['num2'] =$num2;
					$post['num3'] =$num3;
					$post['uptimes'] =time();
					$res = $this->fun->getdatabasesdetail("db_factory_salereport"," AND custom= $custom AND model=$model AND size='".$rs['size']."' AND gender=$gender  AND goods_id=".$rsdb['skuID']);
					if($res)
					{
						$this->db->update('db_factory_salereport',$post," id ='".$res['id']."'");
					}else{
						$this->db->insert('db_factory_salereport',$post);
					}
			}
		}
		return 1;
	}
	function getEuropeorder($gender,$goods_sku)
	{
		$daylist = $this->getdata();
		$i = 0;
		foreach($this->size as $kt=>$sizes)
		{
			$datalist = "";
			foreach($daylist as $key=>$rs)
			{
				$sql = " SELECT sum(orderNum) as orderNums FROM db_factory_sell A LEFT JOIN db_factory_order B ON A.orderId = B.id WHERE A.goods_sku='".$goods_sku."' AND custom = 1 AND model =1 AND gender1 = $gender and orderTime >='".$rs['starttime']."' AND orderTime <='".$rs['endtime']."' AND B.status !=5  AND B.updateflag = 9 AND A.size = $sizes AND B.orderTime >='2017-05-23 18:20:01'";
				$res = $this->db->get_one($sql);
				$datalist[]['orderNums'] = $res['orderNums']?$res['orderNums']:0;
			}
				
				
					$newdatalist[$i]["size"] = $this->sizest[$sizes];
					$newdatalist[$i]["orderNums1"] = $datalist[0]['orderNums'];
					$newdatalist[$i]["orderNums2"] = $datalist[1]['orderNums'];
					$newdatalist[$i]["orderNums3"] = $datalist[2]['orderNums'];
			
			$i=$i+1;
		}
		
		return $newdatalist;
	}

	/************************
		获取需要统计的日期
	*************************/
	function getdata()
	{
		$saleskumonth  = trim($this->config->item('saleskumonth'));
		if($saleskumonth)
		{
			$days = explode("|",$saleskumonth);
			foreach($days as $key => $rs)
			{
				$starttime = date("Y-m-d",time()-$rs*86400)." 00:00:00";
				$endtime = date("Y-m-d")." 23:59:59";
				
				$oneDay[$rs]["starttime"]= $starttime;
				$oneDay[$rs]["endtime"] = $endtime;
			}
			
		}else{
			$days[] =30;
			$days[] =60;
			$days[] =90;
			foreach($days as $key => $rs)
			{
				$starttime = date("Y-m-d",time()-$rs*86400)." 00:00:00";
				$endtime = date("Y-m-d")." 23:59:59";
				
				$oneDay[$rs]["starttime"]= $starttime;
				$oneDay[$rs]["endtime"] = $endtime;
			}

		}
		return $oneDay;


	}

}

/* End of file Goodsreport_m.php */